package com.wu.store.dao;

import com.wu.store.entity.Goods;
import com.wu.store.entity.GoodsWithImgList;
import com.wu.store.entity.Img;
import com.wu.store.entity.SearchRecord;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface GoodsDao {

	@Select("<script>" +
		        "select * from v_goods_category_order " +
		        "<where>" +
		        "price between #{minPrice} and #{maxPrice}" +
		        "<if test='status != null'>" +
		        " and status = #{status}" +
		        "</if>" +
		        "<if test='categoryId != null'>" +
		        " and category_id = #{categoryId} " +
		        "</if>" +
		        "<if test='keyword != null'>" +
		        " and (name regexp '${keyword}' or category_name regexp '${keyword}' )" +
		        "</if>" +
		        "</where>" +
		        "order by sell_count desc, id desc" +
		        "</script>")
	List<Goods> list(Integer status, Long categoryId, Integer minPrice, Integer maxPrice, String keyword);

	@Select("select * from v_goods_category_order order by sell_count desc limit 5")
	List<Goods> listOrderBySellCount();

	@Select("select g.*, p.name as paramsName, b.name as brandName, sum(c.count) as sellCount " +
		        "from t_goods g " +
		        "    left join t_params p on g.brandId = p.id " +
		        "    left join t_brand b on g.brandId = b.id " +
		        "    left join t_order_child c on c.goods_id = g.id " +
		        "where g.id = #{id}")
	GoodsWithImgList selectById(Long id);

	@Select("<script>" +
		        "select * from t_goods " +
		        "<where>" +
		        " name = #{name}" +
		        "<if test='status != null'>" +
		        " and status = #{status}" +
		        "</if>" +
		        "</where>" +
		        "</script>")
	Goods selectByName(String name, Integer status);

	@Insert("insert into t_goods (category_id, name, price, status, img, paramsValue, paramsId, brandId) values (#{categoryId}, #{name}, #{price}, #{status}, #{img}, #{paramsValue}, #{paramsId}, #{brandId})")
	@SelectKey(statement = "select last_insert_id()", keyProperty = "id", before = false, resultType = Long.class)
	void add(Goods goods);

	@Update("<script>" +
		        "update t_goods set " +
		        " category_id = #{categoryId}, name = #{name}, price = #{price}, status = #{status}" +
		        "<if test='paramsValue != null'>" +
		        " , paramsValue = #{paramsValue}" +
		        "</if>" +
		        "<if test='paramsId != null'>" +
		        " , paramsId = #{paramsId} " +
		        "</if>" +
		        "<if test='brandId != null'>" +
		        " , brandId = #{brandId} " +
		        "</if>" +
		        "<if test='img != null'>" +
		        ", img = #{img}" +
		        "</if>" +
		        "where id = #{id}" +
		        "</script>")
	Integer update(Goods goods);

	@Select("select keyword from t_search_record where uid = #{uid} order by id desc")
	List<String> getSearchKeyword(Long uid);

	@Insert("<script>" +
		        "insert into t_search_record (uid, category_id, keyword) values (#{uid}, #{category_id}, #{keyword})" +
		        "</script>")
	void saveSearchRecord(Long uid, Long category_id, String keyword);

	@Select("<script>" +
		        "select * from t_search_record where keyword = #{keyword} and uid = #{uid}" +
		        "</script>")
	List<SearchRecord> getRecordIdList(String keyword, Long uid);

	@Select("select * from t_goods_img i where i.goods_id = #{goodsId}")
	List<Img> getImgList(Long goodsId);


	@Delete("<script>" +
		        "delete from t_goods_img " +
		        "<where>" +
		        "goods_id = #{goodsId}" +
		        "<if test='idList != null'>" +
		        " and id not in (<foreach collection='idList' item='id' separator=','>#{id}</foreach>)" +
		        "</if>" +
		        "</where>" +
		        "</script>")
	void deleteGoodsImg(Long goodsId, List<Long> idList);

	@Insert("<script>" +
		        "insert into t_goods_img (goods_id, img) values " +
		        "<foreach collection='imgList' item='item' separator=','>" +
		        "(#{goodsId}, #{item.img})" +
		        "</foreach>" +
		        "</script> ")
	int updateGoodsImg(Long goodsId, List<Img> imgList);

	@Update("update t_goods_img set img = #{img} where id = #{id}")
	int updateImgItem(Img img);
}
